package com.znf4.controller.good;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import com.znf4.base.BaseReturnModel;
import com.znf4.constants.CommonConst;
import com.znf4.good.enums.GoodStatusEnum;
import com.znf4.enums.OperationStatusEnum;
import com.znf4.good.free.entity.Free;
import com.znf4.good.free.server.FreeServer;
import com.znf4.id.IdGenerator;
import com.znf4.utils.OSSFile;

/**
 * 商品信息
 */
@RestController
@RequestMapping("/good/free")
public class FreeGoodController {

	private static final Logger LOG = LoggerFactory.getLogger(FreeGoodController.class);

	@Autowired
	private FreeServer freeServer;

	@Autowired
	private IdGenerator idGenerator;

	/**
	 * 函数功能说明 ： 添加免费抢购商品
	 *
	 * @参数： @return @return String @throws
	 */
	@RequestMapping(value = "/add", method = { RequestMethod.POST, RequestMethod.GET })
	public Map<String, Object> add(Free free, @RequestParam(value = "file", required = false)List<MultipartFile> files) {

		try {
			free.setId(idGenerator.getGoodNameNumber());

			String fileName = CommonConst.FREE_GOOD_IMG_NUMBER + free.getId();

			// 文件上传OSS
			for (MultipartFile multipartFile : files) {
				String orignalFileName = multipartFile.getOriginalFilename();
				if (multipartFile != null && orignalFileName != null && orignalFileName.length() > 0) {
					free.setImg(OSSFile.upFile(multipartFile, fileName));
					LOG.info("免费商品上传图片到oss.....并设置数据库参数。");
				}
			}
			free.setStatus(GoodStatusEnum.FROZEN.getDesc()); // 状态
			freeServer.addFreeGood(free);
		} catch (Exception e) {
			LOG.info(e.getMessage());
			return BaseReturnModel.getMapModel("faild", "", OperationStatusEnum.ERROR.getDesc());
		}
		return BaseReturnModel.getMapModel("success", "操作完成", "");

	}

	/**
	 * 函数功能说明 ： 修改免费抢购商品
	 *
	 * @参数： @return @return String @throws
	 */
	@RequestMapping(value = "/update", method = { RequestMethod.POST, RequestMethod.GET })
	public Map<String, Object> update(Free free , @RequestParam(value = "file", required = false)List<MultipartFile> files) {

		try {
			String fileName = CommonConst.FREE_GOOD_IMG_NUMBER + free.getId();
			// 文件上传OSS
			for (MultipartFile multipartFile : files) {
				String orignalFileName = multipartFile.getOriginalFilename();
				if (multipartFile != null && orignalFileName != null && orignalFileName.length() > 0) {
					free.setImg(OSSFile.upFile(multipartFile, fileName));
					LOG.info("免费商品上传图片到oss.....并设置数据库参数。");
				}
			}
			freeServer.updateFreeGood(free);
		} catch (Exception e) {
			LOG.info("修改免费商品信息 :", e.getMessage());
			return BaseReturnModel.getMapModel("faild", "", OperationStatusEnum.ERROR.getDesc());
		}
		return BaseReturnModel.getMapModel("success", "操作完成", "");
	}

	/**
	 * 函数功能说明 ： 删除免费抢购商品
	 *
	 * @参数： @return @return String @throws
	 */
	@RequestMapping(value = "/delOne", method = { RequestMethod.POST, RequestMethod.GET })
	public Map<String, Object> delOne(Free free, Model model) {

		try {
			freeServer.delOneFreeGood(free);
			// 删除文件 OSS
			OSSFile.deleteFile(free.getImg());
		} catch (Exception e) {
			LOG.info("删除单个其他图片 :", e.getMessage());
			return BaseReturnModel.getMapModel("faild", "", OperationStatusEnum.ERROR.getDesc());
		}
		return BaseReturnModel.getMapModel("success", "操作完成", "");

	}

	/**
	 * 函数功能说明 ： 删除多条免费抢购商品
	 *
	 * @参数： @return @return String @throws
	 */
	@RequestMapping(value = "/delAll", method = { RequestMethod.POST, RequestMethod.GET })
	public Map<String, Object> delAll(String[] ids, Model model) {
		try {
			List<Free> lfree = new ArrayList<>();
			for (int i = 0; i < ids.length; i++) {
				Free free = new Free();
				free.setId(ids[i]);
				lfree.add(free);
				// 删除文件
				Free delFree = freeServer.queryOne(free);
				OSSFile.deleteFile(delFree.getImg());
			}
			freeServer.delAllFreeGood(lfree);
			model.addAttribute("status", OperationStatusEnum.SUCCESS.getDesc());
		} catch (Exception e) {
			LOG.info("删除多个其他商品信息:", e.getMessage());
			return BaseReturnModel.getMapModel("faild", "", OperationStatusEnum.ERROR.getDesc());
		}
		return BaseReturnModel.getMapModel("success", "操作完成", "");
	}
	
	
	
	/**
	 * 函数功能说明 ： 获取所有免费商品
	 *
	 * @参数： @return @return String @throws
	 */
	@RequestMapping(value = "/queryAll", method = { RequestMethod.POST, RequestMethod.GET })
	public Map<String, Object> queryAll(Free free) {
		List<Free> list = freeServer.queryAll(free);
		return BaseReturnModel.getMapModel("success", list, "");
	}
	
	
	
	

}
